package com.sgzn.sgpay.service.tcp.model.terminal.request;

import com.sgzn.sgpay.service.tcp.SgTcpProtocolHead;
import com.sgzn.sgpay.service.tcp.util.SgTcpUtil;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;

/**
 * 设置终端参数(兑币机) 请求类
 * @author huangsenguang
 * @version Id: TcpSetTerminalParamExchangeRequest.java, v 0.1 2018/7/23 11:49 huangsenguang Exp $$
 */
@Data
public class TcpSetTerminalParamExchangeRequest extends TcpCommonRequest{
    /**
     * 长度
     */
    private String length="0E";

    /**
     * 索引
     */
    private String index="01";

    /**
     * 命令
     */
    private String cmd="03";

    /**
     * Data[0-1]:1元出币
     */
    @NotBlank
    private String data1;

    /**
     * Data[2-3]:5元出币
     */
    @NotBlank
    private String data2;

    /**
     * Data[4-5]:10元出币
     */
    @NotBlank
    private String data3;

    /**
     * Data[6-7]:20元出币
     */
    @NotBlank
    private String data4;

    /**
     * Data[8-9]:50元出币
     */
    @NotBlank
    private String data5;

    /**
     * Data[10-11]:100元出币
     */
    @NotBlank
    private String data6;

    @Override
    public String toString(){
        //根据协议规范重载toString方法
        return beginCode + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                serialCode + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                deviceId + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                commandReq + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                head + length + index + cmd + getData() +
                getAllCheck(length + index + cmd + getData()) + end + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                checkSumCode + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                endCode;
    }

    /**
     * 获取校验码
     * @param needToCheck
     * @return
     */
    private String getAllCheck(String needToCheck) {
        this.setCheck(SgTcpUtil.getToCheck(needToCheck));
        return this.check;
    }

    /**
     * 获取Data 数据
     *   Data[0]:自增加码。
     Data[1-2]:投币数。
     Data[3-4]:金额

     * @return
     */
    private String getData() {
        return getConvertResult(data1) + getConvertResult(data2) + getConvertResult(data3) +getConvertResult(data4) +getConvertResult(data5) + getConvertResult(data6);
    }

    /**
     * 将低位在前的字节数组转换成16进制字符串
     * @param sourceString
     * @return
     */
    private String getConvertResult(String sourceString) {
//        return SgTcpUtil.bytes2HexString(SgTcpUtil.IntToByteArray(Integer.parseInt(sourceString)));
        return SgTcpUtil.intStringToHexString(sourceString, 4);
    }


    public static void main(String[] args){
        TcpSetTerminalParamExchangeRequest tcpIncreaseCreditRequest = new TcpSetTerminalParamExchangeRequest();
        tcpIncreaseCreditRequest.setDeviceId("10001");
        tcpIncreaseCreditRequest.setData1("00");
        tcpIncreaseCreditRequest.setData2("44");
        tcpIncreaseCreditRequest.setData3("00");
        tcpIncreaseCreditRequest.setData4("33");
        tcpIncreaseCreditRequest.setData5("22");
        tcpIncreaseCreditRequest.setData6("11");
        System.out.println(tcpIncreaseCreditRequest.toString());
    }
}

